#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from lxml import etree
import logging

from cvirt.cnode.libvirtXmlObject.baseDomain import XmlObject

LOG = logging.getLogger(__name__)

"""
    @author jay.han
"""
class CPUFeature(XmlObject):
    ROOT_NAME = "feature"
    def __init__(self, parse_xml=None, **kw):
        super(CPUFeature, self).__init__(parse_xml=parse_xml,**kw)

        self.name = None

    def _parse_xml(self, xmldoc):
        self.name = xmldoc.get("name")

    def _get_xml_config(self, root):
        ft = root

        ft.set("name", self.name)

        return ft

class CPU(XmlObject):
    ROOT_NAME = 'cpu'
    def __init__(self, parse_xml=None, **kw):
        super(CPU, self).__init__(parse_xml=parse_xml,**kw)

        self.arch = None
        self.vendor = None
        self.model = None

        self.sockets = None
        self.cores = None
        self.threads = None

        self.features = []

    def _parse_xml(self, xmldoc):
        for c in xmldoc.getchildren():
            if c.tag == "arch":
                self.arch = c.text
            elif c.tag == "model":
                self.model = c.text
            elif c.tag == "vendor":
                self.vendor = c.text
            elif c.tag == "topology":
                self.sockets = int(c.get("sockets"))
                self.cores = int(c.get("cores"))
                self.threads = int(c.get("threads"))
            elif c.tag == "feature":
                f = CPUFeature()
                f._parse_xml(c)
                self.add_feature(f)

    def _get_xml_config(self, root):
        cpu = root

        if self.arch is not None:
            cpu.append(self._set_text("arch", self.arch))
        if self.model is not None:
            cpu.append(self._set_text("model", self.model))
        if self.vendor is not None:
            cpu.append(self._set_text("vendor", self.vendor))

        if (self.sockets is not None and
                    self.cores is not None and
                    self.threads is not None):
            top = etree.Element("topology")
            top.set("sockets", str(self.sockets))
            top.set("cores", str(self.cores))
            top.set("threads", str(self.threads))
            cpu.append(top)

        for f in self.features:
            cpu.append(f.get_xml_config())

        return cpu

    def add_feature(self, feat):
        self.features.append(feat)


class GuestCPUFeature(CPUFeature):

    def __init__(self, parse_xml=None, **kw):

        self.policy = "require"
        self.name = None
        super(GuestCPUFeature, self).__init__(parse_xml=parse_xml,**kw)

    def _get_xml_config(self, root):
        ft = root

        ft.set("policy", self.policy)
        ft.set("name", self.name)
        return ft


class GuestCPU(CPU):
    def __init__(self, parse_xml=None, **kw):
        super(GuestCPU, self).__init__(parse_xml=parse_xml,**kw)

        self.mode = None
        self.match = "exact"

    def _get_xml_config(self, root):

        cpu = super(GuestCPU,self)._get_xml_config(root)

        if self.mode:
            cpu.set("mode", self.mode)
        cpu.set("match", self.match)

        return cpu
